package us.icitap.cm.utils;



import java.lang.reflect.Field;

import javax.persistence.Transient;

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;

import us.icitap.cm.business.entities.BaseEntity;



public class MyCustomizer implements DescriptorCustomizer {
	public void customize(ClassDescriptor descriptor) {
	

		Field[] fields =  descriptor.getJavaClass().getDeclaredFields();
		int indx = 1;
		
		for (int i = 0; i < fields.length; i++) {
			if(!fields[i].getName().equals("serialVersionUID") && !fields[i].isAnnotationPresent(Transient.class)){
				
				if(descriptor.getMappingForAttributeName(fields[i].getName()) != null){
					descriptor.getMappingForAttributeName(fields[i].getName()).setWeight(indx);
					indx+=1;
				}
				
			}
		}
		
		Class superClass = descriptor.getJavaClass().getSuperclass();
		if (BaseEntity.class == superClass){
			
			fields = superClass.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				if(!fields[i].getName().equals("serialVersionUID")  && !fields[i].isAnnotationPresent(Transient.class)){
				
					if(descriptor.getMappingForAttributeName(fields[i].getName()) != null){
						descriptor.getMappingForAttributeName(fields[i].getName()).setWeight(indx);
						indx+=1;
					}
				}
			}
	
			
		}

	}
}

